using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace MyGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Runner : GameObject
    {
        public Texture2D TextureBlood;
        public bool IsDied;
        public bool IsMainPlayer;

        private Color color;
        private double lastX;

        public Runner(Game game)
            : base(game)
        {
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            color = Color.Gray;

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (IsDied)
            {
                Y += GameGlobals.backgroundVelocity;
                if (color.A >= 200)
                {
                    color.A -= 2;
                }
            }
            else if (!IsMainPlayer)
            {
                //TODO: random move
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            if (IsDied)
                spriteBatch.Draw(TextureBlood, position, color);

            spriteBatch.Draw(Texture, position, null, Color.White, 0.0f, Vector2.Zero,
                    1f, lastX > X ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0f);

            spriteBatch.End();

            lastX = X;

            base.Draw(gameTime);
        }

        public void SetAtCenter()
        {
            X = (GameGlobals.Width / 2) - (Texture.Width / 2);
            Y = (GameGlobals.Height / 2) - (Texture.Height / 2);
        }
    }
}
